/***
 *  C++ new /delete
 * 
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>

class Book { 
    std::string m_title; 
    int m_pages; 
public: 
    // 构造函数：初始化书籍信息 
    Book(std::string t, int p) { 
        m_title = t;
        m_pages = p;

        std::cout << "构造函数调用："<<" 新书《" << m_title << "》诞生，共" << m_pages << "页" << std::endl; 
    } 
    Book(){
        std::cout << "构造函数调用" << std::endl;
    }
    ~Book(){
        std::cout << "析构函数调用" << std::endl;
    }
}; 

class Blanks
{
    char m_c;
    int m_a;
    double m_d;
public:
    Blanks(){
        std::cout << "Blanks 构造函数调用" << std::endl;
        std::cout << "Blanks m_c:" << m_c<<std::endl;
    }
    ~Blanks(){
        std::cout << "Blanks 析构函数调用" << std::endl;
    }
    void *operator new( size_t stAllocateBlock, char chInit );

    void operator delete(void* ptr);
};
void *Blanks::operator new( size_t stAllocateBlock, char chInit )
{
    // 编译器根据类大小，默认传入值；
    void *pvTemp = malloc( stAllocateBlock );
    std::cout << "Blanks Size:" << stAllocateBlock <<std::endl;
    if( pvTemp != 0 )
        memset( pvTemp, chInit, stAllocateBlock );
    return pvTemp;
}

void Blanks::operator delete(void* ptr) {  
    std::free(ptr);  
    std::cout << "Blanks 自定义delete释放内存" << std::endl;  
}  

int main()
{
    
    Book* pbook = new Book; // 分配内存 + 调用构造函数  

    Book* pArrBook = new Book[3];

    char buffer[sizeof(Book)];  
    Book* p_placement = new(buffer)Book; // 在buffer内存位置构造Book对象  

    Blanks *a5 = new('a') Blanks;
    delete a5;
    return 0;
}